# SPDX-License-Identifier: Apache-2.0

if(DEFINED TOOLCHAIN_HOME)
  # When Toolchain home is defined, then we are cross-compiling, so only look
  # for linker in that path, else we are using host tools.
  set(LLD_SEARCH_PATH PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH)
endif()

find_program(CMAKE_LINKER     ld.lld ${LLD_SEARCH_PATH} REQUIRED)

set_ifndef(LINKERFLAGPREFIX -Wl)

set_property(GLOBAL PROPERTY TOPT "-Wl,-T")

# Run $LINKER_SCRIPT file through the C preprocessor, producing ${linker_script_gen}
# NOTE: ${linker_script_gen} will be produced at build-time; not at configure-time
macro(configure_kernel_linker_script linker_script_gen linker_pass_define)
  set(extra_dependencies ${ARGN})

  # Using DEPFILE with other generators than Ninja is an error.
  set(linker_script_dep DEPFILE ${linker_script_gen}.dep)

  seminix_get_include_directories_for_lang(C current_includes kernel_interface)
  get_property(current_defines GLOBAL PROPERTY PROPERTY_LINKER_SCRIPT_DEFINES)

  add_custom_command(
    OUTPUT ${linker_script_gen}
    DEPENDS
    ${SEMINIX_SOURCE_DIR}/arch/${ARCH}/kernel/linker.lds.S
    ${extra_dependencies}
    # NB: 'linker_script_dep' will use a keyword that ends 'DEPENDS'
    ${linker_script_dep}
    COMMAND ${CMAKE_C_COMPILER}
    -x assembler-with-cpp
    -undef
    -MD -MF ${linker_script_gen}.dep -MT ${linker_script_gen}
    -D_LINKER
    -D_ASMLANGUAGE
    ${current_includes}
    ${current_defines}
    ${linker_pass_define}
    -E ${SEMINIX_SOURCE_DIR}/arch/${ARCH}/kernel/linker.lds.S
    -P # Prevent generation of debug `#line' directives.
    -o ${linker_script_gen}
    BYPRODUCTS
    ${linker_script_gen}.dep
    VERBATIM
    WORKING_DIRECTORY ${SEMINIX_BINARY_DIR}
    COMMAND_EXPAND_LISTS
  )
endmacro()
